#pragma once
#include "logger.hpp"
#include <string>
#include <iostream>
#include <mysql/mysql.h>
#include <memory>
#include<jsoncpp/json/json.h>
#include<sstream>
#include<vector>
#include<fstream>

class UtilMysql
{
public:
    // 创建mysql句柄
    static MYSQL *mysql_create(const std::string &host,
                               const std::string &username,
                               const std::string &password,
                               const std::string &dbname,
                               const uint16_t port = 3306)
    {
        // 1.初始化句柄
        MYSQL *mysql = mysql_init(NULL);
        if (mysql == NULL)
        {
            // 初始化失败
            ELOG(" mysql_init failed");
            return NULL;
        }
        // 2.连接服务器
        if (mysql_real_connect(mysql, host.c_str(), username.c_str(), password.c_str(), dbname.c_str(), port, NULL, 0) == NULL)
        {
            // 连接服务器失败
            ELOG("mysql_real_connect failed");
            return NULL;
        }
        // 3.设置字符集
        if (mysql_set_character_set(mysql, "utf8") != 0)
        {
            // 设置失败
            ELOG("mysql_set_character_set failed");
            return NULL;
        }
        // 4.选择数据库，在2中已经做过

        return mysql;
    }

    static bool mysql_exec(MYSQL *mysql, const std::string &sql)
    {
        int ret = mysql_query(mysql, sql.c_str());
        if (ret != 0)
        {
            // 查询失败
            ELOG("%s\n", sql.c_str());
            ELOG("mysql_query failed : %s\n", mysql_errno(mysql));
            return false;
        }
        return true;
    }

    static void mysql_destroy(MYSQL *mysql)
    {
        if (mysql != NULL)
        {
            mysql_close(mysql);
        }
    }
};

class UtilJson
{
public:
    static bool serialize(const Json::Value& root , std::string& str)
    {
        //1实例化工厂对象
        Json::StreamWriterBuilder swb; 

        //2.用工厂对象创建出StreamWriter对象
        std::unique_ptr<Json::StreamWriter> sw(swb.newStreamWriter()); 
        std::stringstream ss; 

        //把root对象里的数据序列化到ss
        int ret = sw->write(root,&ss);
        if(ret != 0)
        {
            //序列化失败
            ELOG("sw->write failed");
            return false;
        }
        str = ss.str();
        return true;
    }
    static bool unserialize(const std::string& str, Json::Value& root)
    {
        Json::CharReaderBuilder crb;
        std::unique_ptr<Json::CharReader> cr(crb.newCharReader()); 
        std::string err ; 

        bool ret = cr->parse(str.c_str(),str.c_str()+str.size(),&root,&err);
        if(!ret)
        {
             ELOG("cr->parse failed : %s" , err.c_str());
             return false;
        }
        return true;

    }

};

class UtilString
{
public:
    static int split(const std::string& src, const std::string& sep ,std::vector<std::string>& res)
    {
        size_t pos , idx = 0 ;
        while(idx < src.size())
        {
            //1.从idx位置开始查找子串的下标
            pos = src.find(sep,idx); 
            if(pos == std::string::npos)
            {
                //没找到
                res.push_back(src.substr(idx));
                break;
            }
            //123,,,,,333遇上连续的逗号
            if(pos == idx)
            {
                idx += sep.size();
                continue;
            }
            //从idx开始 push pos - idx个字节
            res.push_back(src.substr(idx,pos-idx));
            idx = pos + sep.size(); //跳到分隔字符的后一个字符
        }
        return res.size(); // 返回分割了多少个子串
    }
};


class UtilFile
{
public: 
  static bool read(std::string& filename , std::string& body,size_t& size)
  {
      //把filename文件读入body字符串中
      //1.打开文件 
      std::ifstream ifs(filename,std::ios::binary); //二进制打开 
      if(!ifs.is_open())
      {
        //文件打开失败 
        ELOG("%s open failed!" , filename.c_str());
        return false;
      }
      //获取文件大小 
      size_t fsize = 0 ;
      ifs.seekg(0,std::ios::end); //位置指针移动到末尾 
      fsize = ifs.tellg(); //获取偏移量
      ifs.seekg(0,std::ios::beg); 
      
      body.resize(fsize); //设置body大小 
      ifs.read(&body[0],fsize); //读入数据到body中 

      if(!ifs.good())
      {
        ELOG("%s read failed!" , filename.c_str());
        ifs.close();
        return false;
      }
      size = fsize;
      ifs.close();
      return true;
  }
};
